layui.define
(function (exports) {
	/* 定义object对象【start】 */
	var object = {
		init:function (c) {this.f(c);this.load();return object;},
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s = new Date().getTime(),t = this;
			if(!c.id_grid_panel) {
				alert('表格容器的ID属性未指定，请检查！');
			}
			if(!c.id_table_main) {
				alert('主表ID属性未指定，请检查！');
			}
			if(!c.id) {
				alert('搜索框容器ID属性未指定，请检查！');
			}
			/* ys_list_search_form基本配置，格式：
			{
				"id": "id_form_for_search",
				"conditions": [{}]
			}
			 */
			t.c = c;
			t.c.id = c.id;
			t.c.id_grid_panel = c.id_grid_panel;
			t.c.id_table_main = c.id_table_main;
			console.log('conditions---', t.c.conditions);
		},
		load:function () { // 加载控件
			var t = this;
			var h = this.loadForm();
			$('#'+this.c.id).html(h);
			this.initFormItem();
			this.listenBtn();
		},
		loadForm:function () { // 加载表单
	    	var t = this;
			var h = ``;
				h += `<form class="layui-form margin_0" data-id="form-for-search" style="padding: 0.2rem;">`;
					h += `<div class="layui-form-item margin_0">`;
					for(var i=0;i<t.c.conditions.length;i++){
						h += this.loadFormItem(t.c.conditions[i], i);
					}
					h += `</div>`;
					
					h += this.loadFormBtn();
				h += `</form>`;
			return h;
		},
		loadFormItem:function (condition, index) { // 加载单个表单
			var h = ``;
			if(condition.type == 't_hidden') {
				h = this.loadFormItemHidden(condition, index);
			} else if(condition.type == 't_text') {
				h = this.loadFormItemInput(condition, index);
			} else if(condition.type == 't_year') {
				h = this.loadFormItemYear(condition, index);
			} else if(condition.type == 't_monthly') {
				h = this.loadFormItemMonth(condition, index);
			} else if(condition.type == 't_date') {
				h = this.loadFormItemDate(condition, index);
			} else if(condition.type == 't_datetime') {
				h = this.loadFormItemDatetime(condition, index);
			} else if(condition.type == 't_dropdown') {
				h = this.loadFormItemDropdown(condition, index);
			} else if(condition.type == 't_buttontext') {
				h = this.loadFormItemButtonText(condition, index);
			}
			return h;
		},
		loadFormItemHidden:function (condition, index) { // 加载表单-隐藏域
			var h =
				`<input name="` + condition.code + `" type="hidden" cond-range-type="` + condition.rangeType + `" value="` + condition.value + `">`;
			return h;
		},
		loadFormItemInput:function (condition, index) { // 加载表单-输入框
			var h =
				`<div class="layui-inline layui-col-xs3 layui-col-md3 margin_0" data-index="` + index + `">`
					+ `<label class="layui-form-label padding_2 width_80 text_overflow_ellipsis">` + condition.name + `:</label>`
					+ `<div class="layui-input-block display_flex height_24 line_height_24 min_height_24 margin_l_90">`
						+ `<input data-index="` + index + `" data-filter="ysui_input_text" data-code="` + condition.code + `" type="text" autocomplete="off" placeholder="` + condition.placeHolder + `" cond-range-type="` + condition.rangeType + `" value="` + (condition.value ? condition.value : '') + `" class=" height_24 layui-input" style="border: solid 0.1px; border-color: #B5B5B5; width:90%;">`
						+ `<i class="layui-icon layui-icon-close-fill float_right" title="清空此条件" data-index="` + index + `"></i>`
					+ `</div>`
				+ `</div>`;
			return h;
		},
		loadFormItemYear:function (condition, index) { // 加载表单-年份选择框（YYYY）
			var h =
				`<div class="layui-inline layui-col-xs3 layui-col-md3 margin_0">`
					+ `<label class="layui-form-label padding_2 width_80 text_overflow_ellipsis">` + condition.name + `:</label>`
					+ `<div class="layui-input-block display_flex height_24 line_height_24 min_height_24 margin_l_90">`
						+ `<input readonly data-index="` + index + `" data-filter="ysui_input_year" data-code="` + condition.code + `" type="text" autocomplete="off" placeholder="` + condition.placeHolder + `" cond-range-type="` + condition.rangeType + `" value="` + (condition.value ? condition.value : '') + `" class=" height_24 layui-input" style="border: solid 0.1px; border-color: #B5B5B5; width:90%;">`
						+ `<i class="layui-icon layui-icon-close-fill" title="清空此条件" data-index="` + index + `"></i>`
					+ `</div>`
				+ `</div>`;
			return h;
		},
		loadFormItemMonth:function (condition, index) { // 加载表单-月份选择框（YYYY-MM）
			var h =
				`<div class="layui-inline layui-col-xs3 layui-col-md3 margin_0">`
					+ `<label class="layui-form-label padding_2 width_80 text_overflow_ellipsis">` + condition.name + `:</label>`
					+ `<div class="layui-input-block display_flex height_24 line_height_24 min_height_24 margin_l_90">`
						+ `<input readonly data-index="` + index + `" data-filter="ysui_input_month" data-code="` + condition.code + `" type="text" autocomplete="off" placeholder="` + condition.placeHolder + `" cond-range-type="` + condition.rangeType + `" value="` + (condition.value ? condition.value : '') + `" class=" height_24 layui-input" style="border: solid 0.1px; border-color: #B5B5B5; width:90%;">`
						+ `<i class="layui-icon layui-icon-close-fill" title="清空此条件" data-index="` + index + `"></i>`
					+ `</div>`
				+ `</div>`;
			return h;
		},
		loadFormItemDate:function (condition, index) { // 加载表单-日期选择框（YYYY-MM-DD）
			var h =
				`<div class="layui-inline layui-col-xs3 layui-col-md3 margin_0">`
					+ `<label class="layui-form-label padding_2 width_80 text_overflow_ellipsis">` + condition.name + `:</label>`
					+ `<div class="layui-input-block display_flex height_24 line_height_24 min_height_24 margin_l_90">`
						+ `<input readonly data-index="` + index + `" data-filter="ysui_input_date" data-code="` + condition.code + `" type="text" autocomplete="off" placeholder="` + condition.placeHolder + `" cond-range-type="` + condition.rangeType + `" value="` + (condition.value ? condition.value : '') + `" class=" height_24 layui-input" style="border: solid 0.1px; border-color: #B5B5B5; width:90%;">`
						+ `<i class="layui-icon layui-icon-close-fill" title="清空此条件" data-index="` + index + `"></i>`
					+ `</div>`
				+ `</div>`;
			return h;
		},
		loadFormItemDatetime:function (condition, index) { // 加载表单-时间选择框（HH:MM:SS）
			var h =
				`<div class="layui-inline layui-col-xs3 layui-col-md3 margin_0">`
					+ `<label class="layui-form-label padding_2 width_80 text_overflow_ellipsis">` + condition.name + `:</label>`
					+ `<div class="layui-input-block display_flex height_24 line_height_24 min_height_24 margin_l_90">`
						+ `<input data-index="` + index + `" readonly data-code="` + condition.code + `" type="text" autocomplete="off" placeholder="` + condition.placeHolder + `" cond-range-type="` + condition.rangeType + `" value="` + (condition.value ? condition.value : '') + `" class=" height_24 layui-input" style="border: solid 0.1px; border-color: #B5B5B5; width:90%;">`
						+ `<i class="layui-icon layui-icon-close-fill" title="清空此条件" data-index="` + index + `"></i>`
					+ `</div>`
				+ `</div>`;
			return h;
		},
		loadFormItemDropdown:function (condition, index) { // 加载表单-下拉选择框
			var h = ``;
				h += `<div class="layui-inline layui-col-xs3 layui-col-md3 margin_0">`;
					h += `<label class="layui-form-label padding_2 width_80 text_overflow_ellipsis">` + condition.name + `:</label>`;
					h += `<div class="layui-input-block display_flex height_24 line_height_24 min_height_24 margin_l_90">`
						h += `<select data-code="` + condition.code + `" class="bo-search-field-selector" cond-range-type="` + condition.rangeType + `">`;
						for (var i = 0; i < condition.optionArr.length; i++) {
							var option = condition.optionArr[i];
							h += `<option value="` + option.value + `">` + option.name + `</option>`;
						}
						h += `</select>`;
					h += `</div>`;
				h += `</div>`;
			return h;
		},
		loadFormItemButtonText:function (condition, index) { // 加载表单-弹出选择框
			var h =
				`<div class="layui-inline layui-col-xs3 layui-col-md3 margin_0">`
					+ `<label class="layui-form-label padding_2 width_80 text_overflow_ellipsis">` + condition.name + `:</label>`
					+ `<div class="layui-input-block display_flex height_24 line_height_24 min_height_24 margin_l_90">`
						+ `<input data-index="` + index + `" readonly data-id-field-value="` + condition.id + `" type="hidden" data-code="` + condition.code + `" cond-range-type="` + condition.rangeType + `" value="` + (condition.value ? condition.value : '') + `" class="bo-search-field">`
						+ `<input data-index="` + index + `" readonly data-id-field-name="` + condition.id + `" type="text" placeholder="` + condition.placeHolder + `" autocomplete="off" class="height_24 layui-input" value="" style="border: solid 0.1px; border-color: #B5B5B5; width:90%;">`
						+ `<i class="layui-icon layui-icon-close-fill" title="清空此条件" data-index="` + index + `"></i>`
						+ `<i style="position: relative; right: 40px;" class="layui-icon layui-icon-more" th:onclick="showModal('` + condition.id + `')"></i>`
					+ `</div>`
				+ `</div>`;
			return h;
		},
		loadFormBtn:function () { // 加载按钮
			var h =
				`<div style="width:100%; text-align: center;">`
					+ `<a data-filter="filter_form_search" class="layui-btn layui-btn-normal layui-btn-xs search_btn"><i class="layui-icon layui-icon-search"></i> 查询</a>`
					+ `<button data-filter="filter_form_clear" type="button" class="layui-btn layui-btn-danger layui-btn-xs" lay-filter="filter_form_search_clear"><i class="layui-icon layui-icon-close"></i> 清空</button>`
				+ `</div>`;
			return h;
		},
		initFormItem:function () { // 控件初始化
			var form = layui.form;
			var laydate = layui.laydate;
			form.render('select');
			var t = this;
			
			// 监听输入框变化
			$('#' + t.c.id).find('input[data-filter="ysui_input_text"]').on('change',function (e) {
				var v = $(this).val();
				var index = $(this).attr('data-index');
				
				t.c.conditions[index].value = v;
			});
			
			// 初始化年份选择框
			$('#' + t.c.id).find('input[data-filter="ysui_input_year"]').each(function(){
				var index = $(this).attr('data-index');
				laydate.render({
					elem: this,
					type: 'year',
					//, max: maxtime//设置选择最大时间为当前
					calendar: false,
					btns: ['clear', 'confirm'],
					done: function (value, date) {//value是选中的值
						//选中后
						t.c.conditions[index].value = value;
					}
				});
			});
			
			// 初始化月份选择框
			$('#' + t.c.id).find('input[data-filter="ysui_input_month"]').each(function(){
				console.log(this);
				var index = $(this).attr('data-index');
				var condRangeType = $(this).attr('cond-range-type');
				if(condRangeType == 't_system') {
					var myDate = new Date();
					var maxtime = myDate.getFullYear() + "-" + (myDate.getMonth() + 1);
					laydate.render({
						elem: this
						, type: 'month'
						, format: 'yyyy-MM'
						, max: maxtime
						, calendar: true
						, btns: ['clear', 'confirm', 'now']
						, done: function (value, date) {
							//选中后
							t.c.conditions[index].value = value;
						}
					});
				} else if(condRangeType == 't_between') {
					var myDate = new Date();
					var maxtime = myDate.getFullYear() + "-" + (myDate.getMonth() + 1);
					laydate.render({
						elem: this
						, type: 'month'
						, format: 'yyyy-MM'
						, range: '~'
						, max: maxtime
						, calendar: true
						, btns: ['clear', 'confirm', 'now']
						, done: function (value, date) {
							console.log(value);
							//选中后
							t.c.conditions[index].value = value;
						}
					});
				}
			});
			
			// 初始化日期选择框（YYYY-MM-DD）
			$('#' + t.c.id).find('input[data-filter="ysui_input_date"]').each(function(){
				var index = $(this).attr('data-index');
				var condRangeType = $(this).attr('cond-range-type');
				if(condRangeType == 't_system') {
					var myDate = new Date();
					var maxtime = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + (myDate.getDate());
					laydate.render({
						elem: this
						, type: 'date'
						, max: maxtime
						, calendar: true
						, btns: ['clear', 'confirm', 'now']
						, done: function (value, date) {
							//选中后
							t.c.conditions[index].value = value;
						}
					});
				} else if(condRangeType == 't_between') {
					var myDate = new Date();
					var maxtime = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + (myDate.getDate());
					laydate.render({
						elem: this
						, type: 'date'
						, max: maxtime
						, range: '~'
						, calendar: true
						, btns: ['clear', 'confirm', 'now']
						, done: function (value, date) {
							console.log(value);
							//选中后
							t.c.conditions[index].value = value;
						}
					});
				}
			});
	    },
		listenBtn:function () { // 添加监听
			var t = this;
	        // 监听“查询”按钮
			$('#' + t.c.id).find('a[data-filter="filter_form_search"]').on('click',function (e) {
				console.log(t.getConds());
				layui.table.reload(t.c.id_table_main, {
					page: {curr: 1},
					where: {'conditions': JSON.stringify(t.getConds())}
				})
			});
			// 监听各个“清空”图标
			$('#' + t.c.id).find('i.layui-icon-close-fill').on('click',function (e) {
				var index = $(this).attr('data-index'); // 当前索引
				
				var condition = t.c.conditions[index];
				
				// 清除当前搜索条件的值
				$('input[data-code="' + condition.code + '"]').val('');
				t.c.conditions[index].value = '';
				t.setConds(t.c.conditions);
				return;
			});
			// 监听“清空”按钮
			$('#' + t.c.id).find('button[data-filter="filter_form_clear"]').on('click',function (e) {
				layer.confirm("确定清空搜索条件吗？",{
					btn: ['确定', '取消']
				}, function (index) {
					// 表单数据清空
					$('#' + t.c.id).find('input[type=text],select').each(function() {
						$(this).val('');
					});
					// 搜索条件数据清空（隐藏条件不处理）
					for (var i = 0; i < t.c.conditions.length; i++) {
						var condition = t.c.conditions[i];
						if(condition.type == 't_hidden') {
							continue;
						} else {
							t.c.conditions[i].value = '';
						}
					}
					t.setConds(t.c.conditions);
					layer.close(index);
				});
				return;
			});
	    },
		getData:function () {
			return this.c.conditions;
		},
		setConds:function (conditions) {
			this.c.conditions = conditions;
		},
		getConds:function () {
			var conditionsRes = [];
			for (var i = 0; i < this.c.conditions.length; i++) {
				var condition = this.c.conditions[i];
				if(condition.value) {
					conditionsRes.push({'rangeType':condition.rangeType, 'value': condition.value, 'field':condition.code});
				}
			}
			return conditionsRes;
		}
	};
	/* 定义object对象【end】 */
	
	
	exports('ys_modal_list_search_form', object);
});